package org.wbd.pklib.web.beans;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.wbd.pklib.db.Item;
import org.wbd.pklib.db.Loan;
import org.wbd.pklib.db.User;
import org.wbd.pklib.db.logic.LoanLogic;
import org.wbd.pklib.db.logic.LogicRespond;

public class LoanBean {
	private List<Item> itemList;
	private List<Item> loanList;
	private User user;
	private String opinion;
	private String isbn;
	private String title;
	private String owner;
	private Item selectedItem;

	public LoanBean() {
		FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
	}

	public List<Item> getLoanList() {
		return loanList;
	}

	public void setLoanList(List<Item> loanList) {
		this.loanList = loanList;
	}

	public List<Item> getItemList() {
		itemList = new ArrayList<Item>();
		itemList.add(getItem());
		return itemList;
	}

	public Item getItem() {
		SearchBean bean = (SearchBean) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("searchBean");
		return bean.getSelectedItem();
	}


	public String getOpinion() {
		return opinion;
	}

	public void setOpinion(String opinion) {
		this.opinion = opinion;
	}

	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public void setUser(User user) {
		this.user = user;
	}

	
	
	public void setSelectedItem(Item selectedItem) {
		this.selectedItem = selectedItem;
	}

	public String borrow() {
		LoanLogic loanLogic = LoanLogic.getInstance();
		LogicRespond respond = loanLogic.LoanBook(user, getItem());
		if (respond.isSuccesful()) {
			FacesMessage message = new FacesMessage("Borrow sucessful!");
			FacesContext.getCurrentInstance().addMessage(null, message);
			UserBean bean= (UserBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("userBean");
			bean.getLoanList().add(getItem());
		} else {
			FacesMessage message = new FacesMessage("Borrow failed!");
			FacesContext.getCurrentInstance().addMessage(null, message);
		}
		return "menu?faces-redirect= true";
	}

	public String redirect(){
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedItem", selectedItem);
		return "opinion.xhtml?faces-redirect= true";
	}
	
	public String returnBook(){
		LoanLogic logic= LoanLogic.getInstance();
		Item item= (Item)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedItem");
		Loan loan= (Loan)logic.getLoanByItem(item).getResultObject();
		LogicRespond respond= logic.returnBook(loan, opinion);
		if (respond.isSuccesful()) {
			FacesMessage message = new FacesMessage("Return sucessful!");
			FacesContext.getCurrentInstance().addMessage(null, message);
			UserBean bean= (UserBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("userBean");
			bean.getLoanList().remove(item);
		} else {
			FacesMessage message = new FacesMessage("Return failed!");
			FacesContext.getCurrentInstance().addMessage(null, message);
		}
		return "myLoans?faces-redirect= true";
	}
			
	public String back() {
		return "menu?faces-redirect= true";
	}

}
